package com.example.smartwaterapi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartwaterapi.entity.UserAchievement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户成就Mapper接口
 */
@Mapper
public interface UserAchievementMapper extends BaseMapper<UserAchievement> {
    
    /**
     * 获取用户已完成的成就
     */
    @Select("SELECT ua.*, wa.name, wa.description, wa.icon, wa.badge_image " +
            "FROM user_achievements ua " +
            "JOIN water_achievements wa ON ua.achievement_id = wa.id " +
            "WHERE ua.user_id = #{userId} AND ua.is_completed = 1 " +
            "ORDER BY ua.completed_at DESC")
    List<Map<String, Object>> getUserCompletedAchievements(@Param("userId") Long userId);
    
    /**
     * 获取用户进行中的成就
     */
    @Select("SELECT ua.*, wa.name, wa.description, wa.icon, wa.condition_value " +
            "FROM user_achievements ua " +
            "JOIN water_achievements wa ON ua.achievement_id = wa.id " +
            "WHERE ua.user_id = #{userId} AND ua.is_completed = 0 " +
            "ORDER BY ua.current_progress DESC")
    List<Map<String, Object>> getUserInProgressAchievements(@Param("userId") Long userId);
    
    /**
     * 获取用户未领取奖励的成就
     */
    @Select("SELECT ua.*, wa.name, wa.description, wa.icon, wa.reward_points, wa.reward_title " +
            "FROM user_achievements ua " +
            "JOIN water_achievements wa ON ua.achievement_id = wa.id " +
            "WHERE ua.user_id = #{userId} AND ua.is_completed = 1 AND ua.rewards_claimed = 0")
    List<Map<String, Object>> getUserUnclaimedRewards(@Param("userId") Long userId);
    
    /**
     * 更新成就进度
     */
    @Update("UPDATE user_achievements SET " +
            "current_progress = #{progress}, " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND achievement_id = #{achievementId}")
    int updateAchievementProgress(@Param("userId") Long userId, 
                                @Param("achievementId") Long achievementId, 
                                @Param("progress") Integer progress);
    
    /**
     * 完成成就
     */
    @Update("UPDATE user_achievements SET " +
            "is_completed = 1, " +
            "completed_at = #{completedAt}, " +
            "completion_count = completion_count + 1, " +
            "last_completed_at = #{completedAt}, " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND achievement_id = #{achievementId}")
    int completeAchievement(@Param("userId") Long userId, 
                          @Param("achievementId") Long achievementId, 
                          @Param("completedAt") LocalDateTime completedAt);
    
    /**
     * 领取成就奖励
     */
    @Update("UPDATE user_achievements SET " +
            "rewards_claimed = 1, " +
            "claimed_at = NOW(), " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND achievement_id = #{achievementId} AND is_completed = 1")
    int claimAchievementReward(@Param("userId") Long userId, @Param("achievementId") Long achievementId);
    
    /**
     * 获取用户成就统计
     */
    @Select("SELECT " +
            "COUNT(CASE WHEN is_completed = 1 THEN 1 END) as completed_count, " +
            "COUNT(CASE WHEN is_completed = 0 THEN 1 END) as in_progress_count, " +
            "COUNT(CASE WHEN is_completed = 1 AND rewards_claimed = 0 THEN 1 END) as unclaimed_count " +
            "FROM user_achievements WHERE user_id = #{userId}")
    Map<String, Object> getUserAchievementStats(@Param("userId") Long userId);
} 